package jp.co.cos.ittrick.channel.controller.client;

import java.util.List;

import jp.co.cos.ittrick.channel.meta.PageModelMeta;
import jp.co.cos.ittrick.channel.model.ChapterModel;
import jp.co.cos.ittrick.channel.model.PageModel;
import jp.co.cos.ittrick.channel.service.ChapterService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class GetPageListController extends Controller {

    private ChapterService chapterService;

    public GetPageListController() {
        chapterService = new ChapterService();
    }

    @Override
    protected Navigation run() throws Exception {

        String chapter = asString("chapter");

        ChapterModel chapterModel = chapterService.getChapterByKey(Integer.valueOf(chapter));
        List<PageModel> pageList = chapterModel.getPageModelListRef().getModelList();

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        Object[] entityArray = pageList.toArray();
        response.getWriter().println(PageModelMeta.get().modelsToJson(entityArray));
        response.flushBuffer();

        return null;
    }

}
